package net.xju3_4.dao;

import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.entity.Vehicles;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class VehiclesDao {
    public List<Vehicles> list() {
        // 操作数据库
        // 获取连接
        Connection connection = JdbcUtilsV1.getConnection();
        String sql = "select * from vehicles";
        List<Vehicles> vehiclesList = JdbcUtilsV1.executeQuery(connection, Vehicles.class, sql);
        return vehiclesList;
    }

    public void insert(Vehicles vehicles) {
        String sql = "INSERT INTO vehicles(plate_Number,brand,model,color,year,seats,vehicle_Type,purchase_Date," +
                "insurance_Expiry,status,daily_Rate,description,images) " +
                "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        JdbcUtilsV1.executeUpdate(sql, vehicles.getPlatenNumber(),
                vehicles.getBrand(),vehicles.getModel(),vehicles.getColor(),vehicles.getYear(),vehicles.getSeats(),
                vehicles.getVehicleType(),vehicles.getPurchaseDate(),vehicles.getInsuranceExpiry(),vehicles.getStatus(),
                vehicles.getDailyRate(),vehicles.getDescription(),vehicles.getImages());
    }

    public Vehicles selectOne(int id) {
        String sql = "select id,plate_number platenNumber,brand,model,color,year,seats," +
                "vehicle_Type vehicleType,purchase_Date purchaseDate,insurance_Expiry insuranceExpiry,status,daily_Rate dailyRate,description,images" +
                " from vehicles where id=?";
        return JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(), Vehicles.class, sql, id);
    }

    public void deleteById(int id) {
        // 逻辑删除，实际执行update
        String sql = "delete from vehicles where id = ?";
        JdbcUtilsV1.executeUpdate(sql, id);
    }

    public void update(Vehicles vehicles) {
        StringBuilder sql = new StringBuilder("UPDATE vehicles SET ");
        List<Object> params = new ArrayList<>();

        // 动态拼接非空字段
        if (vehicles.getPlatenNumber() != null &&!vehicles.getPlatenNumber().isEmpty()) {
            sql.append( "plate_Number = ?, ");
            params.add(vehicles.getPlatenNumber());
        }

        if (vehicles.getBrand() != null &&!vehicles.getBrand().isEmpty()) {
            sql.append( "brand = ?, ");
            params.add(vehicles.getBrand());
        }
        if (vehicles.getModel() != null&&!vehicles.getModel().isEmpty()) {
            sql.append( "model = ?, ");
            params.add(vehicles.getModel());
        }
        if (vehicles.getColor() != null&&!vehicles.getColor().isEmpty()) {
            sql.append( "color = ?, ");
            params.add(vehicles.getColor());
        }
        if (vehicles.getYear() != null&&vehicles.getYear() != 0) {
            sql.append( "year = ?, ");
            params.add(vehicles.getYear());
        }
        if (vehicles.getSeats() != null&&vehicles.getSeats() != 0) {
            sql.append( "seats = ?, ");
            params.add(vehicles.getSeats());
        }
        if (vehicles.getVehicleType() != null&&!vehicles.getVehicleType().isEmpty()) {
            sql.append( "vehicle_Type = ?, ");
            params.add(vehicles.getVehicleType());
        }
        if (vehicles.getPurchaseDate() != null) {
            sql.append( "purchase_Date = ?, ");
            params.add(vehicles.getPurchaseDate());
        }
        if (vehicles.getInsuranceExpiry() != null) {
            sql.append( "insurance_Expiry = ?, ");
            params.add(vehicles.getInsuranceExpiry());
        }
        if (vehicles.getStatus() != null&&!vehicles.getStatus().isEmpty()) {
            sql.append( "status = ?, ");
            params.add(vehicles.getStatus());
        }
        if (vehicles.getDescription() != null&&!vehicles.getDescription().isEmpty()) {
            sql.append( "description = ?, ");
            params.add(vehicles.getDescription());
        }
        if (vehicles.getImages() != null&&!vehicles.getImages().isEmpty()) {
            sql.append( "images = ?, ");
            params.add(vehicles.getImages());
        }
        if (vehicles.getDailyRate() != null&&vehicles.getDailyRate() != 0.0) {
            sql.append( "daily_Rate = ?, ");
            params.add(vehicles.getDailyRate());
        }
        // 移除最后的逗号和空格
        if (!params.isEmpty()) {
            sql.delete(sql.length() - 2, sql.length());
        }
        // 添加WHERE条件
        sql.append(" WHERE id = ?");
        params.add(vehicles.getId());
        JdbcUtilsV1.executeUpdate(sql.toString(),params.toArray());
    }

    public PageInfo<Vehicles> pageList(String platenNumber, String model, Integer pageNo, Integer pageSize) {
        String sql = "select id,plate_number platenNumber,brand,model,color,year,seats," +
                "vehicle_Type vehicleType,purchase_Date purchaseDate,insurance_Expiry insuranceExpiry,status,daily_Rate dailyRate,description,images" +
                " from vehicles where 1 = 1 ";
        List<Object> params = new ArrayList<Object>();
        // 拼接sql 条件查询
        if (platenNumber != null && !platenNumber.isEmpty()) {
            sql += " and plate_Number like ?";
            params.add("%" + platenNumber + "%");
        }
        if (model != null && !model.isEmpty()) {
            sql += " and model = ?";
            params.add(model);
        }
        sql += " limit ?,?";
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);
        List<Vehicles> vehicless = JdbcUtilsV1.executeQuery(Vehicles.class, sql, params.toArray());
        // 分页
        String countSql = "select count(*) from vehicles where 1 = 1 ";
        long count = JdbcUtilsV1.executeQueryOneValue(countSql);
        PageInfo<Vehicles> pageInfo = new PageInfo<>(pageNo, pageSize, (int) count, vehicless);
        return pageInfo;
    }
}
